package com.iwhalecloud.bss.kite.cucc.order.accept.chain.impl;

import com.iwhalecloud.bss.kite.client.api.ord.IHandleManService;
import com.iwhalecloud.bss.kite.client.dto.cust.CustDTO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.TableConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.common.util.SeqUtils;
import com.iwhalecloud.bss.kite.cucc.order.accept.chain.AbstractOrderHandler;
import com.iwhalecloud.bss.kite.dataservice.entity.OrderHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.order.client.context.OrderContext;
import com.iwhalecloud.bss.kite.order.client.dto.OrderRequest;
import com.ztesoft.bss.base.staticdata.CoreDataInteract;
import com.ztesoft.bss.common.util.SpringUtil;
import com.ztesoft.bss.order.base.consts.KeyValues;
import com.ztesoft.bss.order.base.consts.KeyValuesLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 经办人处理
 */
@Component
public class OrderHandlerInfoHandler extends AbstractOrderHandler {

    @Autowired
    IHandleManService handleManService;

    @Override
    public void doHandle(OrderRequest request) {
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        KiteAttr kiteAttr = querySceneInstService.getCommonAttr(request.getMainSceneInstId(), KeyConsts.HANDLER_CUST);
        if (ObjectUtils.isEmpty(kiteAttr)) {
            return;
        }
        if (KiteStringUtils.isEmpty(kiteAttr.getValue())) {
            return;
        }
        this.createOrderHandler(request, kiteAttr);
    }

    private void createOrderHandler(OrderRequest request, KiteAttr kiteAttr) {
        OrderHandler orderHandler = new OrderHandler();
        if (kiteAttr.getValueDesc() != null) {
            String[] valueDesc = kiteAttr.getValueDesc().split(" ");
            if (valueDesc.length > 1) {
                orderHandler.setHandlerPhone(valueDesc[1]);
                orderHandler.setHomePhone(valueDesc[1]);
            }
        }
        orderHandler.setOrderHandlerId(SeqUtils.getNext(TableConsts.OrderHandler));
        orderHandler.setCustId(request.getCustId());
        orderHandler.setCustOrderId(request.getCustOrderId());
        orderHandler.setStatusCd(CoreDataInteract.EVT_C_0014_1100);
        orderHandler.setStatusDate(request.getAcceptDate());
        orderHandler.setCreateDate(request.getAcceptDate());
        orderHandler.setStatusDate(request.getAcceptDate());
        orderHandler.setUpdateDate(request.getAcceptDate());
        orderHandler.setCreateStaff(request.getStaffId());
        orderHandler.setCustOrderId(request.getCustOrderId());
        orderHandler.setCustId(request.getCustId());
        orderHandler.setOrderType(KeyValues.ORDER_TYPE_CUSTORDER);
        orderHandler.setFinishTime(request.getAcceptDate());
        orderHandler.setRelationTypeCd(KeyValuesLocal.PARTY_TYPE_006);
        orderHandler.setPartyType(KeyValuesLocal.PARTY_TYPE_006);
        String image = handleManService.getCustImage(request.getCustId());
        if (KiteStringUtils.isNotEmpty(image)) {
            orderHandler.setCertInfo(image);
        }

        CustDTO custDTO = OrderContext.getCust(kiteAttr.getValue());
        if (KiteStringUtils.equals(KeyConsts.ACCEPT_MODE_YYPL, request.getAcceptMode())) {
            custDTO = OrderContext.getCust(request.getCustId());
        }
        if (Objects.nonNull(custDTO)) {
            orderHandler.setHandler(custDTO.getCustName());
            orderHandler.setCertType(custDTO.getCertType());
            orderHandler.setCertNum(custDTO.getCertiNumber());
            orderHandler.setPartyId(kiteAttr.getValue());
        }

        // 组装
        List<OrderHandler> OrderHandlers = new ArrayList<>();
        OrderHandlers.add(orderHandler);
        Map<String, List<OrderHandler>> orderHandlerMap = new HashMap<>();
        orderHandlerMap.put(request.getCustOrderId(), OrderHandlers);
        request.getOrderContainer().getOrderHandlerMap().put(request.getCustOrderId(), OrderHandlers);

        // 设置订单项的经办人id
        request.getOrderContainer().getOrderItemMap().forEach((orderItemId, orderItem) -> {
            orderItem.setOrderHandlerId(orderHandler.getOrderHandlerId());
        });
    }
}
